#include "ads1115.h"
#include "HAL_OLED.h"



void ADS1115_Reg_Read(I2C_HandleTypeDef *hi2c, uint8_t addr, uint8_t reg, uint16_t* data)
{
	addr &= (~ 0x01);//发送时在地址的最后一位写0;
	HAL_I2C_Master_Transmit(hi2c, addr, &reg, 1, HAL_MAX_DELAY);
	
	addr |= 0x01;////接收时在地址的最后一位写0;
	uint8_t DataBuffer[2];
	HAL_I2C_Master_Receive(hi2c, addr, DataBuffer, 2, HAL_MAX_DELAY);
	
	*data = (DataBuffer[0]<<8 | DataBuffer[1]);
}

void ADS1115_Reg_Write(I2C_HandleTypeDef *hi2c, uint8_t addr, uint8_t reg, uint16_t* data)
{
	addr &= ~(0x01);

	uint8_t DataFrame[3];
	
	DataFrame[0] = reg;
	DataFrame[1] = (*data) >> 8;
	DataFrame[2] = (*data) & 0xff;
	
	HAL_I2C_Master_Transmit(hi2c, addr, DataFrame, 3, HAL_MAX_DELAY);
}

void ADS1115_Set_PGA(I2C_HandleTypeDef* hi2c, uint8_t addr, ADS1115_FSR_SELECT fsr)
{
	addr &= ~(0x01);
	
	//get data of config register;
	uint16_t Config_Reg; 
	ADS1115_Reg_Read(hi2c, addr, ADS1115_CONFIG_REGISTER_ADDR, &Config_Reg);
	
	//set fsr 
	Config_Reg &= (0xf1ff);
	
	Config_Reg |= (fsr<<9);
	ADS1115_Reg_Write(hi2c, addr, ADS1115_CONFIG_REGISTER_ADDR, &Config_Reg);
}

void ADS1115_Set_MUX(I2C_HandleTypeDef* hi2c, uint8_t addr, ADS1115_Mux_SELECT mux)
{
	addr &= ~(0x01);
	
	//get data of config register;
	uint16_t Config_Reg; 
	ADS1115_Reg_Read(hi2c, addr, ADS1115_CONFIG_REGISTER_ADDR, &Config_Reg);
	
	//set fsr 
	Config_Reg &= (0x8fff);
	
	Config_Reg |= (mux<<12);
	ADS1115_Reg_Write(hi2c, addr, ADS1115_CONFIG_REGISTER_ADDR, &Config_Reg);
}

void ADS1115_Set_MODE(I2C_HandleTypeDef* hi2c, uint8_t addr, ADS1115_Mode_SELECT mode)
{
	addr &= ~(0x01);
	
	//get data of config register;
	uint16_t Config_Reg; 
	ADS1115_Reg_Read(hi2c, addr, ADS1115_CONFIG_REGISTER_ADDR, &Config_Reg);
	
	//set fsr 
	Config_Reg &= (0xfeff);
	
	Config_Reg |= (mode<<8);
	ADS1115_Reg_Write(hi2c, addr, ADS1115_CONFIG_REGISTER_ADDR, &Config_Reg);
}

void ADS1115_Set_SPS(I2C_HandleTypeDef* hi2c, uint8_t addr, ADS1115_SPS_SELECT sps)
{
	addr &= ~(0x01);
	
	//get data of config register;
	uint16_t Config_Reg; 
	ADS1115_Reg_Read(hi2c, addr, ADS1115_CONFIG_REGISTER_ADDR, &Config_Reg);
	
	//set fsr 
	Config_Reg &= (0xff1f);
	
	Config_Reg |= (sps<<5);
	ADS1115_Reg_Write(hi2c, addr, ADS1115_CONFIG_REGISTER_ADDR, &Config_Reg);
}

double ADS1115_Get_Voltage(I2C_HandleTypeDef* hi2c, uint8_t addr, float range)
{
	//float range = 0;
	
	addr |= 0x01;
	uint16_t data =0;
	ADS1115_Reg_Read(hi2c, addr, ADS1115_CONVERSION_REGISTER_ADDR, &data);
	
//	uint16_t pga = 0;
//	ADS1115_Reg_Read(hi2c, addr, ADS1115_CONFIG_REGISTER_ADDR, &pga);
//	
//	pga &=(0x0f00);
//	pga >>=9;
//	
//	if(pga == ADS1115_FSR_6_144)
//	{
//		range = 6.144*2;
//	}
//	else if(pga == ADS1115_FSR_4_096)
//	{
//		range = 4.096*2;
//	}
//	else if(pga == ADS1115_FSR_2_048)
//	{
//		range = 2.048*2;
//	}
//	else if(pga == ADS1115_FSR_1_024)
//	{
//		range = 1.024*2;
//	}
//	else if(pga == ADS1115_FSR_0_512)
//	{
//		range = 0.512*2;
//	}
//	else if(pga == ADS1115_FSR_0_256)
//	{
//		range = 0.256*2;
//	}
	
	double vol = 1.0f*range/65535 * data;
	
	return vol;
}

void ADS1115_Set_RDY_PIN(I2C_HandleTypeDef* hi2c, uint8_t addr, ADS1115_RDY_PIN state)
{
	addr |= 0x01;//读取配置寄存器
	uint16_t data =0;
	ADS1115_Reg_Read(hi2c, addr, ADS1115_CONFIG_REGISTER_ADDR, &data);

	
	data &=(0xfff0);
	
	data |=0x0008;
	
	addr &= ~(0x01);
	
	ADS1115_Reg_Write(hi2c, addr, ADS1115_CONFIG_REGISTER_ADDR, &data);
	
	data = 0xffff;
	ADS1115_Reg_Write(hi2c, addr, ADS1115_HI_THRESH_REGISTER_ADDR, &data);
	data = 0;
	ADS1115_Reg_Write(hi2c, addr, ADS1115_LO_THRESH_REGISTER_ADDR, &data);
	//ADS1115_Reg_Read(hi2c, addr, ADS1115_CONFIG_REGISTER_ADDR, &data);
	//OLED_ShowNum(2,1,data,5);
}
